/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gestores;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
 *
 * @author radxt
 */
public class XMLLeer{
        File fXmlFile;
        DocumentBuilderFactory dbFactory;  
        DocumentBuilder dBuilder;               
        Document doc;

        public XMLLeer(String file){
            try {
                this.fXmlFile = new File(file);
                this.dbFactory = DocumentBuilderFactory.newInstance();
                this.dBuilder = dbFactory.newDocumentBuilder();
                this.doc = dBuilder.parse(fXmlFile);
                doc.getDocumentElement().normalize();
            } catch (ParserConfigurationException ex) {
                Logger.getLogger(XMLLeer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SAXException ex) {
                Logger.getLogger(XMLLeer.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(XMLLeer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        public int devolverPass(String login){

        NodeList nList = doc.getElementsByTagName("Usuario");
        int i = 0;
        Node nNode = nList.item(i);
        Element e = (Element) nNode;
        while(!e.getAttribute("login").equals(login)){
            nNode = nList.item(i);
            e = (Element ) nNode;
            i++;
        }
        return Integer.parseInt((e.getAttribute("pass")));
        
        }
        
        public int devolverGrupo(String login){
            
        NodeList nList = doc.getElementsByTagName("Usuario");
        int i = 0;
        Node nNode = nList.item(i);
        Element e = (Element) nNode;
        while(!e.getAttribute("login").equals(login)){
            nNode = nList.item(i);
            e = (Element ) nNode;
            i++;
        }
        return Integer.parseInt((e.getAttribute("grupo")));
        
        }
        
        public int devolverCodigo(String login){
        NodeList nList = doc.getElementsByTagName("Usuario");
        int i = 0;
        Node nNode = nList.item(i);
        Element e = (Element) nNode;
        while(!e.getAttribute("login").equals(login)){
            nNode = nList.item(i);
            e = (Element ) nNode;
            i++;
        }
        return Integer.parseInt((e.getAttribute("id")));
        }
        
        public int devolverGrupoporID(String id){
                    NodeList nList = doc.getElementsByTagName("Usuario");
        int i = 0;
        Node nNode = nList.item(i);
        Element e = (Element) nNode;
        while(!e.getAttribute("id").equals(id)){
            nNode = nList.item(i);
            e = (Element ) nNode;
            i++;
        }
        return Integer.parseInt((e.getAttribute("grupo")));
        }
        
        
        
        
       public String devolverNombre(String id) {
        NodeList nList = doc.getElementsByTagName("Usuario");
        int i = 0;
        Node nNode = nList.item(i);
        Element e = (Element) nNode;
        while (!e.getAttribute("id").equals(id)) {
            nNode = nList.item(i);
            e = (Element) nNode;
            i++;
        }
        return e.getAttribute("nombre");
    }
       
              public String devolverLogin(String id) {
        NodeList nList = doc.getElementsByTagName("Usuario");
        int i = 0;
        Node nNode = nList.item(i);
        Element e = (Element) nNode;
        while (!e.getAttribute("id").equals(id)) {
            nNode = nList.item(i);
            e = (Element) nNode;
            i++;
        }
        return e.getAttribute("login");
    }

}

